package com.itisyue.fun.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {
    @Bean("fileIoExecutor")
    public ThreadPoolTaskExecutor fileIoExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors() * 2); // CPU核数×2[9](@ref)
        executor.setMaxPoolSize(200);
        executor.setQueueCapacity(1000);          // 缓冲突发流量[8](@ref)
        executor.setThreadNamePrefix("file-io-"); // 线程命名便于监控
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}